home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 001 / ymodem.arc / VVMODEM.C < prev    next >
Text File  |  1986-05-27  |  6KB  |  258 lines

  1. /*
  2.  *  VMODEM
  3.  *  VMS support for UMODEM and vvrb/vvsb programs
  4.  *
  5.  *    Defined herein are some utility routines to make the UNIX
  6.  *    program UMODEM run under VAX/VMS C:
  7.  *
  8.  *        assign_channel    Calls the VMS System Service $ASSIGN
  9.  *                to assign a channel to a device.
  10.  *                The routine currently has the device
  11.  *                "TT" hardwired into it.
  12.  *        gtty        Gets terminal characteristics, almost
  13.  *                like the UNIX GTTY system call.
  14.  *        filestat    Returns the length of the file.
  15.  *        raw_read    Reads characters from the terminal
  16.  *                without any echoing or interpretation
  17.  *                and with an optional timeout period.
  18.  *        raw_write    Writes a character to the terminal
  19.  *                without any interpretation.
  20.  *        raw_wbuf    Writes a buffer to the terminal
  21.  *                without any interpretation.
  22.  *        stty        Sets terminal characteristics, almost
  23.  *                like the UNIX STTY system call.
  24.  *
  25.  */
  26. #include descrip
  27. #include iodef
  28. #include rms
  29. #include ssdef
  30. #include stdio
  31. #include "vmodem.h"
  32.  
  33. #define  TRUE    1
  34. #define  FALSE    0
  35.  
  36. static char    tt_name[]    = "TT";
  37. static short    tt_chan        = -1;        /*  Terminal channel number  */
  38.  
  39. struct    tt_io_iosb                /*  Terminal I/O IOSB  */
  40. {
  41.     short    status;
  42.     short    byte_count;
  43.     short    terminator;
  44.     short    terminator_size;
  45. };
  46.  
  47. /*
  48.  *    Terminator mask for PASSALL reads.
  49.  *    Permits reads of all possible 8-bit characters.
  50.  */
  51. int    t_mask[32] =  {
  52.     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  53.     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  54.     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  55.     0,  0    };
  56.  
  57. struct    terminator_mask {
  58.     short    size ;
  59.     short    unused ;
  60.     int    *mask ;
  61. }
  62.  
  63. termin_mask    = { 32, 0, t_mask };
  64.  
  65. /*
  66.  *    ASSIGN a channel to the logical name TT, which is usually
  67.  *    the terminal.
  68.  */
  69. assign_channel()
  70. {
  71.     int    status;
  72.     $DESCRIPTOR(tt_descriptor, tt_name);
  73.  
  74.     if (tt_chan == -1)
  75.         status    = sys$assign(&tt_descriptor, &tt_chan, 0, 0);
  76.     else
  77.         status    = SS$_NORMAL;
  78.  
  79.     if (status != SS$_NORMAL || tt_chan == -1)
  80.         error("ASSIGN_CHANNEL:  error in SYS$ASSIGN\n", FALSE);
  81.  
  82.     return;
  83. }
  84.  
  85. /*
  86.  *    Gets terminal information from VMS.
  87.  */
  88. gtty(tt_characteristics)
  89. struct    tt_info    *tt_characteristics;
  90. {
  91.     int                status;
  92.  
  93.     if (tt_chan == -1)
  94.         assign_channel();
  95.  
  96.     status    = sys$qiow(0, tt_chan, IO$_SENSEMODE,
  97.       &(tt_characteristics->dev_modes), NULL, 0,
  98.       &(tt_characteristics->dev_characteristics), 12,
  99.       0, 0, 0, 0);
  100.     if (status != SS$_NORMAL ||
  101.       tt_characteristics->dev_modes.status != SS$_NORMAL)
  102.         error("GTTY:  sense mode QIO error return.\n", FALSE);
  103.  
  104.     return(status);
  105. }
  106.  
  107. /*
  108.  *  FILESTAT
  109.  *  Provide FILE STATistics under VMS
  110.  *
  111.  *    Opens the file and counts the bytes (GROSS!!)
  112.  *    This appears to be the only to get the exact number, given
  113.  *    all of RMS's permutations of file structure.
  114.  *    Returns the number of bytes in the file, returns -1 on error.
  115.  */
  116. long    filestat(name)
  117. char    *name;
  118. {
  119.     FILE        *fin;
  120.     long        bytes = -1;
  121.  
  122.     if (fin = fopen(name, "r")) {
  123.         while (++bytes, getc(fin) != EOF)
  124.             ;
  125.         fclose(fin);
  126.     }
  127.     return bytes;
  128. }
  129.  
  130.  
  131.  
  132. /*
  133.  *    Read NCHAR characters from the terminal without echoing or
  134.  *    interpretation.
  135.  *    If the argument SECONDS is non-zero, use that as the
  136.  *    timeout period in seconds for the read.
  137.  *
  138.  *    NOTE THAT THIS FUNCTION RETURNS AN INT, NOT A CHAR!
  139.  *    That is because of the possibility of a SS$_TIMEOUT return.
  140.  *    Returns SS$_TIMEOUT in case of timeout or other error.
  141.  */
  142. int        raw_read(nchar, charbuf, seconds)
  143. char        *charbuf;
  144. int        nchar;
  145. unsigned    seconds;
  146. {
  147.     short            function;
  148.     int            status;
  149.     struct    tt_io_iosb    iosb;
  150.  
  151.     if (tt_chan == -1)
  152.         assign_channel();
  153.  
  154.     function    = IO$_READVBLK | IO$M_NOECHO | IO$M_NOFILTR;
  155.  
  156.     if (seconds)
  157.         status    = sys$qiow(0, tt_chan, function | IO$M_TIMED,
  158.           &iosb, NULL, 0,
  159.           charbuf, nchar, seconds,
  160.           &termin_mask, NULL, 0);
  161.     else
  162.         status    = sys$qiow(0, tt_chan, function,
  163.           &iosb, NULL, 0,
  164.           charbuf, nchar, 0,
  165.           &termin_mask, NULL, 0);
  166.  
  167.     if (status == SS$_TIMEOUT || iosb.status == SS$_TIMEOUT)
  168.         return(SS$_TIMEOUT);
  169.     else if (status != SS$_NORMAL || iosb.status != SS$_NORMAL)
  170.         return(SS$_TIMEOUT);
  171.  
  172.     return((int)*charbuf);
  173. }
  174.  
  175. /*
  176.  *    Writes a character to the terminal without echoing or
  177.  *    interpretation.
  178.  */
  179. raw_write(c)
  180. char    c;
  181. {
  182.     int            status;
  183.     struct    tt_io_iosb    iosb;
  184.  
  185.     if (tt_chan == -1)
  186.         assign_channel();
  187.  
  188.     status    = sys$qiow(0, tt_chan,
  189.       IO$_WRITEVBLK | IO$M_CANCTRLO | IO$M_NOFORMAT,
  190.       &iosb, NULL, 0,
  191.       &c, 1, 0, 0, 0, 0);
  192.  
  193.     if (status != SS$_NORMAL || iosb.status != SS$_NORMAL)
  194.         error("RAW_WRITE:  write QIO error return.\n", TRUE);
  195.  
  196.     return;
  197. }
  198.  
  199. /*
  200.  *    Writes a buffer to the terminal without echoing or
  201.  *    interpretation.
  202.  */
  203. raw_wbuf(nchar, charbuf)
  204. char        *charbuf;
  205. int        nchar;
  206. {
  207.     int            status;
  208.     struct    tt_io_iosb    iosb;
  209.  
  210.     if (tt_chan == -1)
  211.         assign_channel();
  212.  
  213.     status    = sys$qiow(0, tt_chan,
  214.       IO$_WRITEVBLK | IO$M_CANCTRLO | IO$M_NOFORMAT,
  215.       &iosb, NULL, 0,
  216.       charbuf, nchar, 0, 0, 0, 0);
  217.  
  218.     if (status != SS$_NORMAL || iosb.status != SS$_NORMAL)
  219.         error("RAW_WRITE:  write QIO error return.\n", TRUE);
  220.  
  221.     return;
  222. }
  223.  
  224. /*
  225.  *  Sets terminal information from VMS.
  226.  *     Modified 12-85 Larry Farr/Chuck Forsberg to not use
  227.  *     bad parity returned by VMS 4.
  228.  */
  229. stty(tt_characteristics)
  230. struct    tt_info    *tt_characteristics;
  231. {
  232.     short            *f_ptr, /* *p_ptr, */ *s_ptr;
  233.     int            status;
  234.     struct    tt_mode_iosb    iosb;
  235.  
  236.     if (tt_chan == -1)
  237.         assign_channel();
  238.  
  239. /*
  240.  *    We do the following in order to get a full short, concatenating
  241.  *    two adjacent chars:
  242.  */
  243.     s_ptr    = &(tt_characteristics->dev_modes.t_speed);    /*  Speeds  */
  244.     f_ptr    = &(tt_characteristics->dev_modes.CR_fill);    /*  Fills  */
  245.     /* p_ptr    = &(tt_characteristics->dev_modes.parity_flags); */
  246.  
  247.     status    = sys$qiow(0, tt_chan, IO$_SETMODE,
  248.       &iosb, NULL, 0,
  249.       &(tt_characteristics->dev_characteristics), 12,
  250.       /* *s_ptr, *f_ptr, *p_ptr, 0);    */
  251.       *s_ptr, *f_ptr, 0, 0);
  252.     if (status != SS$_NORMAL || iosb.status != SS$_NORMAL)
  253.         printf("STTY:  set mode QIO returned %d\n", status);
  254.  
  255.     return(status);
  256. }
  257.  
  258.